import 'package:flutter/material.dart';
import 'package:flutter_food/core/model/category_model.dart';
import 'package:flutter_food/core/services/local_repository.dart';
import 'package:flutter_food/ui/pages/home/home_category_item.dart';
import '../../../core/ext/int_extension.dart';

class HomeContent extends StatefulWidget {
  @override
  _HomeContentState createState() => _HomeContentState();
}

class _HomeContentState extends State<HomeContent> {
  List<CategoryModel> _categoryList = [];

  // 加载数据
  @override
  void initState() {
    super.initState();
    LocalRepository.getCategoryData().then((value) {
      setState(() {
        _categoryList = value;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
        padding: EdgeInsets.all(20.px),
        itemCount: _categoryList.length,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
            mainAxisSpacing: 20.px,
            crossAxisSpacing: 20.px,
            childAspectRatio: 1.5),
        itemBuilder: (ctx, index) {
          return HomeCategoryItem(_categoryList[index]);
        });
  }
}
